<?php

namespace Agp\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Agp\CoreBundle\Entity\ItemVenda;

/**
 * Agp\CoreBundle\Entity\Venda
 *
 * @ORM\Table(name="venda")
 * @ORM\Entity(repositoryClass="Agp\CoreBundle\Repository\VendaRepository")
 */
class Venda
{
    /**
     * @var integer $codVenda
     *
     * @ORM\Column(name="cod_venda", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $codVenda;
    
    /**
     * @var float $codVendaTerminal
     *
     * @ORM\Column(name="cod_venda_terminal", type="integer", nullable=true)
     */
    protected $codVendaTerminal;

    /**
     * @var integer $codEmpresa
     *
     * @ORM\ManyToOne(targetEntity="Agp\CoreBundle\Entity\Empresa", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_empresa", referencedColumnName="cod_empresa", onDelete="restrict", nullable=false)
     */
    protected $empresa;

    /**
     * @var integer $codTerminal
     *
     * @ORM\ManyToOne(targetEntity="Agp\CoreBundle\Entity\Terminal", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_terminal", referencedColumnName="cod_terminal", onDelete="restrict", nullable=true)
     */
    protected $terminal;

    /**
     * @var integer $codEvento
     *
     * @ORM\ManyToOne(targetEntity="Agp\CoreBundle\Entity\Evento", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_evento", referencedColumnName="cod_evento", onDelete="restrict", nullable=true)
     */
    protected $evento;
    
    /**
     * @var integer $atendente
     *
     * @ORM\ManyToOne(targetEntity="Agp\CoreBundle\Entity\Atendente", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_atendente", referencedColumnName="cod_atendente", onDelete="restrict", nullable=true)
     */
    protected $atendente;

    /**
     * @var float $contadorGeral
     *
     * @ORM\Column(name="turno", type="integer", nullable=true)
     */
    protected $turno;

    /**
     * @var \DateTime $dtVenda
     *
     * @ORM\Column(name="dt_venda", type="datetime", nullable=false)
     */
    protected $dtVenda;
    
    /**
     *
     * @ORM\OneToMany(targetEntity="ItemVenda", mappedBy="venda", cascade={"all"}, orphanRemoval=true)
     */
    protected $itens;
    
    const SIT_ATIVO = 'Ativo';
    const SIT_INATIVO = 'Inativo';
    const SIT_CANCELADO = 'Cancelado';
    /**
     * @var string $situacao
     *
     * @ORM\Column(name="situacao", type="string", nullable=false)
     */
    protected $situacao;
    
    const TP_TERMINAL = 'Terminal';
    const TP_TERMINAL_CARTAO = 'Terminal Cartao';
    const TP_ECOMMERCE = 'Ecommerce';
    /**
     * @var string $tipo
     *
     * @ORM\Column(name="tipo", type="string", nullable=false)
     */
    protected $tipo;
    
    /**
     * @var object $evento
     *
     * @ORM\ManyToOne(targetEntity="Pedido", fetch="LAZY", inversedBy="vendas")
     * @ORM\JoinColumn(name="cod_pedido", referencedColumnName="cod_pedido", nullable=true, onDelete="Cascade", nullable=true)
     */
    protected $pedido;
    
    public function __construct() {
        $this->dtVenda = new \DateTime;
        $this->itens = new ArrayCollection;
        $this->tipo = self::TP_ECOMMERCE;
    }
    
    public function getValorTotal()
    {
        $total = 0;
        foreach($this->getItens() as $item){
            $total += $item->getValorTotalItem();
        }
        
        return $total;
    }
    
    public function getValorTotalAtivo(){
        $total = 0;
        foreach ($this->itens as $i){
            if ($i->getSituacao() != ItemVenda::SIT_ATIVO){
                $total += $i->getValorTotalItem();
            }
        }
        
        return $total;
    }
    
    public function allItemIsAtivo(){
        $flag = true;
        foreach ($this->itens as $i){
            if ($i->getSituacao() != ItemVenda::SIT_ATIVO){
                $flag = false;
            }
        }
        
        return $flag;
    }
    
    public function allItemIsCancelado(){
        $flag = true;
        foreach ($this->itens as $i){
            if ($i->getSituacao() != ItemVenda::SIT_CANCELADO){
                $flag = false;
            }
        }
        return $flag;
    }

    /**
     * Get codVenda
     *
     * @return integer 
     */
    public function getCodVenda()
    {
        return $this->codVenda;
    }
    
    /**
     * Set empresa
     *
     * @param integer $empresa
     * @return Venda
     */
    public function setEmpresa($empresa)
    {
        $this->empresa = $empresa;
    
        return $this;
    }

    /**
     * Get empresa
     *
     * @return integer 
     */
    public function getEmpresa()
    {
        return $this->empresa;
    }

    /**
     * Set terminal
     *
     * @param integer $terminal
     * @return Venda
     */
    public function setTerminal($terminal)
    {
        $this->terminal = $terminal;
    
        return $this;
    }

    /**
     * Get terminal
     *
     * @return integer 
     */
    public function getTerminal()
    {
        return $this->terminal;
    }

    /**
     * Set evento
     *
     * @param integer $evento
     * @return Venda
     */
    public function setEvento($evento)
    {
        $this->evento = $evento;
    
        return $this;
    }

    /**
     * Get evento
     *
     * @return integer 
     */
    public function getEvento()
    {
        return $this->evento;
    }

    /**
     * Set atendente
     *
     * @param integer $atendente
     * @return Venda
     */
    public function setAtendente($atendente)
    {
        $this->atendente = $atendente;
    
        return $this;
    }

    /**
     * Get atendente
     *
     * @return integer 
     */
    public function getAtendente()
    {
        return $this->atendente;
    }

    public function getTurno() {
        return $this->turno;
    }

    public function setTurno($turno) {
        $this->turno = $turno;
    }

    /**
     * Set dtVenda
     *
     * @param \DateTime $dtVenda
     * @return Venda
     */
    public function setDtVenda($dtVenda)
    {
        $this->dtVenda = $dtVenda;
    
        return $this;
    }

    /**
     * Get dtVenda
     *
     * @return \DateTime 
     */
    public function getDtVenda()
    {
        return $this->dtVenda;
    }
    
    public function getItens() {
        return $this->itens;
    }

    public function addItens($itemCompra) {
        $this->itens->add($itemCompra);
    }
    
    public function getCodVendaTerminal() {
        return $this->codVendaTerminal;
    }

    public function setCodVendaTerminal($codVendaTerminal) {
        $this->codVendaTerminal = $codVendaTerminal;
    }
    
    public function getSituacao() {
        return $this->situacao;
    }

    public function setSituacao($situacao) {
        $this->situacao = $situacao;
    }
    
    public function getTipo() {
        return $this->tipo;
    }

    public function setTipo($tipo) {
        $this->tipo = $tipo;
    }
    
    public function getPedido() {
        return $this->pedido;
    }

    public function setPedido($pedido) {
        $this->pedido = $pedido;
    }

    public static function getSituacaoList(){
        return array(self::SIT_ATIVO => self::SIT_ATIVO,
                     self::SIT_CANCELADO => self::SIT_CANCELADO,
                     self::SIT_INATIVO => self::SIT_INATIVO);
    }

}